<template>
    <div class="">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div >
                <searchrevealing>
                    <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="99px">
                        <el-form-item label="选品时间" prop="date">
                            <el-date-picker v-model="queryParams.date" type="date" placeholder="请选择" clearable
                                style="width: 220px" />
                        </el-form-item>
                        <el-form-item label="产品部" prop="dept">
                            <el-select v-model="queryParams.recruiterDept" placeholder="请选择产品部" clearable
                                @change="deptChange" style="width: 220px">
                                <el-option v-for="dict in deptList" :key="dict.deptId" :label="dict.deptName"
                                    :value="dict.deptId" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="招商人" prop="recruiter">
                            <el-select v-model="queryParams.recruiter" placeholder="请选择招商员" clearable
                                style="width: 220px">
                                <el-option v-for="dict in recruiterList" :key="dict.userId" :label="dict.nickName"
                                    :value="dict.userId" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="品类" prop="category">
                            <el-cascader ref="categoryRef" v-model="queryParams.category" :props="categoryProps"
                                style="width: 220px" @change="handleChange" placeholder="请选择" />
                        </el-form-item>
                        <el-form-item label="是否已建群" prop="groupBuilding">
                            <el-select v-model="queryParams.groupBuilding" placeholder="请选择" clearable
                                style="width: 220px">
                                <el-option v-for="dict in groupBuildingList" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="是否已做链接" prop="link">
                            <el-select v-model="queryParams.link" placeholder="请选择" clearable style="width: 220px">
                                <el-option v-for="dict in linkList" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="是否已售卖" prop="selling">
                            <el-select v-model="queryParams.selling" placeholder="请选择" clearable style="width: 220px">
                                <el-option v-for="dict in sellingList" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="是否已归还" prop="returns">
                            <el-select v-model="queryParams.returns" placeholder="请选择" clearable style="width: 220px">
                                <el-option v-for="dict in returnsList" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleQuery">查询</el-button>
                            <el-button @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </searchrevealing>
                <el-card shadow="never">
                    <template #header>
                        <el-row :gutter="10" class="mb8">
                            <el-col :span="1.5">
                                <el-button type="primary" :disabled="multiple" @click="">导出</el-button>
                            </el-col>
                            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                        </el-row>
                    </template>
                    <el-table v-loading="loading" :data="selectionRecordList" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="序号" type="index" width="55" align="center" />
                        <el-table-column label="商品名称" align="center" prop="styleName" />
                        <el-table-column label="商品编码" align="center" prop="styleCode" />
                        <el-table-column label="商品主图" align="center" prop="productImage" width="100">
                            <template #default="scope">
                                <image-preview :src="scope.row.productImage" :width="50" :height="50" />
                            </template>
                        </el-table-column>
                        <el-table-column label="价格" align="center" prop="price" />
                        <el-table-column label="机制" align="center" prop="liveStreamingMechanism" />
                        <el-table-column label="佣金" align="center" prop="commission" />
                        <el-table-column label="成本价" align="center" prop="costPrice" />
                        <el-table-column label="类目" align="center" prop="categoryName" />
                        <el-table-column label="品牌" align="center" prop="brand">
                            <template #default="scope">
                                {{ handleFilterBrand(scope.row.brand, brandList) }}
                            </template>
                        </el-table-column>
                        <el-table-column label="商品卖点" align="center" prop="sellingPoints" />
                        <el-table-column label="店铺名称" align="center" prop="storeName" />
                        <el-table-column label="招商员" align="center" prop="recruiter" />
                        <el-table-column label="是否建群" align="center" prop="groupBuilding" />
                        <el-table-column label="群名称" align="center" prop="groupName" />
                        <el-table-column label="店铺ID" align="center" prop="shopId" />
                        <el-table-column label="商品ID" align="center" prop="commodityId" />
                        <el-table-column label="专属链接" align="center" prop="saleLink" show-overflow-tooltip>
                            <template #default="scope">
                                <el-link :href="scope.row.saleLink" type="primary" target="_blank">{{ scope.row.saleLink
                                    }}</el-link>
                            </template>
                        </el-table-column>

                        <el-table-column label="售卖状态" align="center" prop="selling" />
                        <el-table-column label="选品时间" align="center" prop="date" />
                        <el-table-column label="出库数量" align="center" prop="outboundNums" />
                        <el-table-column label="还品数量" align="center" prop="stillNums" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
                            <template #default="scope">
                                <el-button link type="primary" @click="">
                                    详情</el-button>
                                <el-button link type="primary" @click="" style="margin-left: 0px !important">
                                    补充链接</el-button>
                                <el-button link type="primary" @click="" style="margin-left: 0px !important">
                                    售卖历史</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize" @pagination="getList" />
                </el-card>
            </div>

        </transition>
    </div>
</template>

<script setup name="ProductSelectionManagement" lang="ts">
import { SelectionSupermarketQueryParams, SelectionSupermarketVO } from '@/api/group/productSelection/types'
import { listSupplierAddProduct } from '@/api/system/supplierAddProducts';
import { listBrand } from '@/api/system/brand';
import { BrandVO } from '@/api/system/brand/types';
import { getproductscategorychildlistbypid } from '@/api/system/supplierAddProducts';
import { getDeptList, getUserById } from '@/api/group/qualityControl'

const queryFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const categoryRef = ref();
const showSearch = ref(true);
const loading = ref(true);
const total = ref(0);
const brandList = ref<BrandVO[]>([]);
const ids = ref<Array<string | number>>([]);
const multiple = ref(true);
const deptList = ref<{ deptId: string; deptName: string; }[]>([])
const recruiterList = ref<{ userId: string; nickName: string; }[]>([])
const selectionRecordList = ref<SelectionSupermarketVO[]>([]);
const queryParams = ref<SelectionSupermarketQueryParams>({
    pageNum: 1,
    pageSize: 10,
    date: undefined,
    recruiterDept: undefined,
    recruiter: undefined,
    category: undefined,
    firstCategory: undefined,
    secondCategory: undefined,
    thirdCategory: undefined,
    fourthCategory: undefined,
    groupBuilding: undefined,
    link: undefined,
    selling: undefined,
    returns: undefined,
})

const groupBuildingList = reactive([
    { label: '测试是否已建群: 是', value: '1' },
    { label: '测试是否已建群：否', value: '2' },
])
const linkList = reactive([
    { label: '测试是否已做链接: 是', value: '1' },
    { label: '测试是否已做链接：否', value: '2' },
])
const sellingList = reactive([
    { label: '测试是否已售卖: 是', value: '1' },
    { label: '测试是否已售卖：否', value: '2' },
])
const returnsList = reactive([
    { label: '测试是否已归还: 是', value: '1' },
    { label: '测试是否已归还：否', value: '2' },
])

/**
 * 获取招商部门信息
 */
const getDeptLists = async () => {
    const res = await getDeptList({ parentId: '1782371081418768385' })
    deptList.value = res.data
}
/**
 *  根据招商部获取招商员
 */
const deptChange = async (event: string) => {
    if (event) {
        const res = await getUserById(event)
        recruiterList.value = res.data
        queryParams.value.recruiter = undefined
    } else {
        recruiterList.value = []
        queryParams.value.recruiter = undefined
    }
}

// 类目请求方法封装
const ategoryQuest = (pid: string | number, level: any, resolve: (data: any) => void) => {
    getproductscategorychildlistbypid({ pid: pid }).then(res => {
        const datas = res.rows.map((value: { categoryId: string, categoryName: string }) => ({
            value: value.categoryId,
            label: value.categoryName,
            // leaf: level >= 3
        }));
        resolve(datas);
    })
}

// 类目props
const categoryProps = reactive({
    lazy: true,
    checkStrictly: true,
    lazyLoad(node: any, resolve: (data: any) => void) {
        const { level } = node
        const pid = !level ? 0 : node.value
        ategoryQuest(pid, level, resolve)
    }
})

// 筛选类目change事件
const handleChange = (val: [string, string, string, string]) => {
    if (val) {

        categoryRef.value.togglePopperVisible()
        queryParams.value.category = val[val.length - 1]
        queryParams.value.firstCategory = val[0]
        queryParams.value.secondCategory = val[1]
        queryParams.value.thirdCategory = val[2]
        queryParams.value.fourthCategory = val[3]
    }
}

/**
 * 获取品牌信息
 */
const getListBrand = async () => {
    const res = await listBrand()
    brandList.value = res.rows;
}
/**
 * 品牌检索value
 * @param para 
 * @param dict 
 */
const handleFilterBrand = (para: string | number | undefined, dict: Array<any>) => {
    let _label = '';
    dict.forEach((item: any) => {
        if (item.id == String(para)) {
            _label = item.brandName;
        }
    });
    return _label;
};
/** 查询列表 */
const getList = async () => {
    try {
        loading.value = true;
        const res = await listSupplierAddProduct(queryParams.value);
        selectionRecordList.value = res.rows;
        total.value = res.total;
        loading.value = false;
    } catch (e) {
    } finally {
        loading.value = false;
    }
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: SelectionSupermarketVO[]) => {
    ids.value = selection.map((item) => item.id);
    multiple.value = !selection.length;
};
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.firstCategory = undefined;
    queryParams.value.secondCategory = undefined;
    queryParams.value.thirdCategory = undefined;
    queryParams.value.fourthCategory = undefined;
    handleQuery();
};
onMounted(() => {
    queryParams.value.pageNum = 1;
    getDeptLists();
    getListBrand();
    getList();
})

</script>
<style lang="scss" scoped></style>
